<?php
class ErrorController extends Yaf_Controller_Abstract {

    private $_config;

    public function init(){
        $this->_config = Yaf_Application::app()->getConfig();
    }

    public function errorAction($exception) {
        /*是否开启报错*/
        $showErrors = $this->_config->application->showErrors;
        /*报错以后记录日志*/
        $error_trace = $exception->getTraceAsString();
        $error_message = $exception->getMessage();
       $this->writeLog(date('Y-m-d'),$error_trace.$error_message);
        if($showErrors){
			$this->_view->Errortype = get_class($exception);
            $this->_view->trace = $error_trace;
            $this->_view->message = $error_message;
			$this->_view->exception = $exception;
        }else{
            header('Content-Type: text/html;Charset=UTF-8');
			//错误跳转地址
//			header('Location:'.$this->_config->application->ErrorUrl);
            $this->redirect('/error/error404');
//            exit("您请求的页面正繁忙，请稍后再试!");
        }
        $this->redirect('/error/error404?error=系统错误，请联系客服解决');
    }

    private function _pageNotFound(){
        $this->getResponse()->setHeader('HTTP/1.0 404 Not Found');
        $this->_view->error = 'Page was not found';
    }

    private function _unknownError(){
        $this->getResponse()->setHeader('HTTP/1.0 500 Internal Server Error');
        $this->_view->error = 'Application Error';
    }
    
    public  function writeLog($filename, $message='') {
        $filename = APPLICATION_PATH . "/public/logs/PhpError{$filename}.log";
        $datime = date("Y-m-d H:i:s")." ";
        file_put_contents($filename, $datime.$message."\n", FILE_APPEND);
    }

    public function error404Action(){
        global $_getAreaNames;
        $this->_title = 'Page was not found';
        $this->_keywords = 'Page was not found';
        $this->_description = 'Page was not found';
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $error=empty($_GET['error']) ? '' :htmlspecialchars(trim($_GET['error']) , ENT_QUOTES ,'utf-8');
        //分类
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",50,20);
        $this->_view->kd = $kd;
        $this->_view->title = $this->_title;
        $this->_view->keywords  = $this->_keywords;
        $this->_view->description = $this->_description;
        $this->_view->cate = $cate;
        $this->_view->error = $error;
        $this->_view->_getAreaNames = $_getAreaNames;
    }
}

